<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2016/10/9
 * Time: 09:43
 */
namespace app\index\controller;

use think\Request;
use think\Session;
use app\index\model\Card;

class Change extends Index
{
    /**
     * 修改密码
     * @return mixed
     */
    public function index()
    {
        $this->checkLogin();
        return $this->fetch('Index/change');
    }

    /**
     * 修改密码
     */
    public function changePwd()
    {
        $this->checkLogin();
        $post = Request::instance()->post();
        $result = $this->validate($post, 'Member.edit');
        if ($result !== true) {
            $this->error($result);
        }
        $o_password = strtoupper(md5($post['o_password']));
        $n_password = strtoupper(md5($post['n_password']));
        $cardId = Session::get('cardId');
        $card = Card::where('Password', $o_password)
            ->where('ID', $cardId)
            ->find();
        if ($card === null) {
            $this->error('旧密码错误');
        } else {
            $res = Card::where('ID', $cardId)
                ->update(['Password' => $n_password]);
            if ($res) {
                $this->success('密码修改成功');
            } else {
                $this->error('密码修改失败');
            }
        }
    }
}